//
//  NKFindDetailViewController.m
//  NewKY
//
//  Created by 徐腾 on 2016/11/13.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKFindDetailViewController.h"
#import "NKFindDetailView.h"
#import <Masonry.h>
#import "NKFindViewController.h"
#import <SVProgressHUD.h>
#import "NetWorkingManager.h"

@interface NKFindDetailViewController () <UIScrollViewDelegate>

/** 滚动视图 */
@property (weak, nonatomic) UIScrollView *bottomScrollView;

/** findDetailView */
@property (nonatomic,strong) NKFindDetailView *findDetailView;

/** 全局按钮 */
@property (weak, nonatomic) UIButton *starButton;

/* 按钮 */
@property (strong, nonatomic) NSMutableArray <UIButton *> *buttons;

/** 中间移动的横线 */
@property (weak, nonatomic) UIView *lineView;

/**
 *  收藏按钮
 */
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation NKFindDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.buttons = [NSMutableArray array];
    
    //设置弹框的时间
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];

    //测试控制器是否跳转成功
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置nav的title的颜色
    self.navigationItem.title = @"医生详情";
    
    //将按钮添加到rightBarButtonItem上
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightButton];
    [self.rightButton addTarget:self action:@selector(guanzhu) forControlEvents:UIControlEventTouchUpInside];
    
    //设置界面
    [self setupUI];
    
}

#pragma mark - 设置界面
- (void)setupUI{
    
    //创建顶部视图
    NKFindDetailView *findDetailView = [[[NSBundle mainBundle] loadNibNamed:@"NKFindDetailsView" owner:nil options:nil] lastObject];
    self.findDetailView = findDetailView;
    
    findDetailView.detailModel = self.detailModel;
    //添加到父视图上
    [self.view addSubview:findDetailView];
    //设置约束
    [findDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(110);
    }];
    
    //创建中间视图
    UIView *midView = [[UIView alloc] init];
    //添加到父视图上
    [self.view addSubview:midView];
    //设置约束
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.view);
        make.top.equalTo(findDetailView.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    //添加左边的按钮
    UIButton *leftBtn = [self createBtnWithTitle:@"接诊条件"];
    //添加到父视图上
    [midView addSubview:leftBtn];
    //设置约束
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.left.top.equalTo(midView);
        make.width.mas_equalTo(self.view.bounds.size.width * 0.5);
    }];
    //添加按钮的点击事件
    [leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    //设置左边按钮的tag值
    leftBtn.tag = 0;
    //将按钮添加到按钮数组里面
    [self.buttons addObject:leftBtn];
    //让视图已加载出来,默认选中左边的按钮
    self.starButton = leftBtn;
    leftBtn.selected = YES;
    
    //添加右边的按钮
    UIButton *rightBtn = [self createBtnWithTitle:@"医生简介"];
    //添加到父视图上
    [midView addSubview:rightBtn];
    //设置约束
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.right.top.equalTo(midView);
        make.width.mas_equalTo(self.view.bounds.size.width * 0.5);
    }];
    //添加按钮的点击事件
    [rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    //设置右边按钮的tag值
    rightBtn.tag = 1;
    //将按钮添加到按钮数组里面
    [self.buttons addObject:rightBtn];
    
    //创建底部的scrollView
    UIScrollView *bottomScrollView = [[UIScrollView alloc] init];
    //设置bottomScrollView的背景颜色
    bottomScrollView.backgroundColor = [UIColor orangeColor];
    //设置禁止弹簧效果
    bottomScrollView.bounces = NO;
    //禁止垂直滚动
    bottomScrollView.showsHorizontalScrollIndicator = NO;
    //开启分页
    bottomScrollView.pagingEnabled = YES;
    //设置代理
    bottomScrollView.delegate = self;
    //添加到父视图上
    [self.view addSubview:bottomScrollView];
    //设置约束
    [bottomScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(midView.mas_bottom).offset(2);
    }];
    
    self.bottomScrollView = bottomScrollView;
    
    
    UITextView *textView1 = [[UITextView alloc] init];
    //添加到父视图上
    [bottomScrollView addSubview:textView1];
    //设置是否可以编辑
    textView1.editable = NO;
    //设置字体大小
    textView1.font = [UIFont systemFontOfSize:18];
    //设置内容
    textView1.text = @"我不是针对谁,我是说在座的各位都是垃圾";
    //设置约束
    [textView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_bottom);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
        make.height.mas_equalTo(522);
    }];
    
    UITextView *textView2 = [[UITextView alloc] init];
    //添加到父视图上
    [bottomScrollView addSubview:textView2];
    //设置是否可以编辑
    textView2.editable = NO;
    //设置字体大小
    textView2.font = [UIFont systemFontOfSize:18];
    //设置内容
    textView2.text = @"你们的皇帝回来了";
    //设置约束
    [textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_bottom);
        make.left.equalTo(textView1.mas_right);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
        make.height.mas_equalTo(522);
    }];
    
    
    //创建下划线
    UIView *lineView = [[UIView alloc] init];
    //0x45  0xb3   0xea
    lineView.backgroundColor = [UIColor colorWithRGB:0x4ebbb8];
    //添加到中间父视图上
    [leftBtn addSubview:lineView];
    //设置约束
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.width.left.mas_equalTo(leftBtn);
        make.bottom.equalTo(leftBtn).offset(2);
    }];
    //属性记录
    self.lineView = lineView;
    
}


/**
 按钮的点击事件

 @param btn 传入进来点击的按钮
 */
- (void)btnAction:(UIButton *)btn{
    
    if(btn!=self.starButton){
        self.starButton.selected=NO;
        self.starButton=btn;
    }
    self.starButton.selected=YES;
    /*------ 1. 移动横线 ------*/
    CGFloat x = 0;
    
    if(btn.tag == 1){
       x = self.view.bounds.size.width / 2;
    }
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(x);
    }];
    
    [self didSelectedIndex:btn.tag];
    
    [UIView animateWithDuration:0.25 animations:^{
        // 立即刷新界面
        [self.view layoutIfNeeded];
    }];

}


/**
 点击按钮让scrollView滚动到对应的textView上去
 */
- (void)didSelectedIndex:(NSUInteger)index{
    
    [self.bottomScrollView setContentOffset:CGPointMake(index * [UIScreen mainScreen].bounds.size.width, 0) animated:YES];
}

//滚动减速的时候调用一次而已
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat x = 0;
    //计算当前滚动的scrollView是哪个界面
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    
    if ( index == 1 ) {
        //滚动scrollView让按钮和按钮下面的线跟着scrollView一起变
        x = self.view.bounds.size.width / 2;
        [self btnAction:self.buttons[1]];
    }else if (index == 0){
        [self btnAction:self.buttons[0]];
        x = 0;
    }
    
    //更新按钮下面线的约束
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(x);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        // 立即刷新界面
        [self.view layoutIfNeeded];
    }];
}

/**
 *  当控制器的根视图的布局已经完成之后, 会触发该方法
 */
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    // 此时, 子视图已经布局完成, 自动布局已经生效
//    NSLog(@"%@", NSStringFromCGRect(self.bottomScrollView.frame));
    
    self.bottomScrollView.contentSize = CGSizeMake(self.bottomScrollView.bounds.size.width * 2, self.bottomScrollView.bounds.size.height);
}


/**
 创建按钮的方法

 @param title 按钮的文字
 @return 返回按钮
 */
- (UIButton *)createBtnWithTitle:(NSString *)title{
    
    UIButton *btn = [[UIButton alloc] init];
    //设置按钮的文字
    [btn setTitle:title forState:UIControlStateNormal];
    //设置按钮文字的颜色
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //设置按钮选中时的颜色
    [btn setTitleColor:[UIColor colorWithRGB:0x4ebbb8] forState:UIControlStateSelected];
    //设置按钮的图片
    [btn setBackgroundImage:[UIImage imageNamed:@"link_button_02"] forState:UIControlStateNormal];
    //设置按钮文字的大小
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    //消除按钮点按下去的阴影
    btn.adjustsImageWhenHighlighted = NO;

    return btn;
}

/**
 nav右边按钮的点击事件
 */
- (void)guanzhu{
    
    NSString *url = @"";
    NSDictionary *parameters = [[NSDictionary alloc] init];
    
    if (!self.rightButton.selected) {
        //关注医生接口
        url = @"http://iosapi.itcast.cn/doctor/addDoctor.json.php";
        parameters = @{@"user_id":@(1000089),@"doctor_id":@(300000315)};
    }
    else {
        //取消关注
        url = @"http://iosapi.itcast.cn/doctor/deleteDoctor.json.php";
        parameters = @{@"user_id":@1000089,@"doctor_id":@300000315};
    }
    
    [NetWorkingManager sendPOSTDataWithPath:url withParamters:parameters withProgress:^(float progress) {
        if (self.rightButton.selected) {
            [SVProgressHUD showProgress:progress status:@"正在关注..."];
        }else{
            [SVProgressHUD showProgress:progress status:@"取消关注..."];
        }
        
    } success:^(BOOL isSuccess, id responseObject) {
        self.rightButton.selected = !self.rightButton.selected;
        
        //取出字典里面的字典
        NSDictionary *dict1 = responseObject[@"data"];
        //字典转模
        NKFindModel *model = [NKFindModel modelWithDictionary:dict1];
        
        // 1.把model传递到关注控制器
        if (self.completion != nil) {
            BOOL isAttention = YES;
            if (self.rightButton.selected == NO) {
                 isAttention = NO;
            }
            
            // 回调主页传入的代码块
            self.completion(model,isAttention);
        }
        [SVProgressHUD showSuccessWithStatus:@"完成操作"];
        
    } failure:^(NSError *error) {
        if (error != nil) {
            [SVProgressHUD showErrorWithStatus:@"您的网络不给力"];
        }
    }];
}

//懒加载
- (UIButton *)rightButton {
    if (_rightButton == nil) {
        _rightButton = [[UIButton alloc] init];
        [_rightButton setImage:[UIImage imageNamed:@"collection_off"] forState:UIControlStateNormal];
        [_rightButton setImage:[UIImage imageNamed:@"collection_on"] forState:UIControlStateSelected];
        _rightButton.frame = CGRectMake(0, 0, 30, 30);
    }
    return _rightButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
